#define _CRT_SECURE_NO_WARNINGS   1

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int max1 = 5e3 + 10;
const int max2 = 5e4 + 10;
vector<int> e[max1];
struct node{
	int u, dis;
};
bool vis[max1];

int bfs(int s, int n){
	queue<node>q;
	q.push(node{ s, 0 });
	while (!q.empty()){
		node temp = q.front();
		q.pop();
		int u = temp.u, dis = temp.dis;
		for (int i = 0; i<e[u].size(); i++){
			if (e[u][i] == n)
				return dis + 1;
			if (!vis[e[u][i]]){
				q.push(node{ e[u][i], dis + 1 });
				vis[e[u][i]] = true;
			}
		}
	}
	return -1;
}
int main(){
	int n, m, u, v;
	cin >> n >> m;
	while (m--){
		cin >> u >> v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	cout << bfs(1, n) << endl;
	return 0;
}